Skip to content

snapenv: Include snap application environment state - #16016

Merged
bboozzoo merged 2 commits into
canonical:masterfrom
3v1n0:snap-app-env-vars
Jun 2, 2026
Merged

snapenv: Include snap application environment state#16016
bboozzoo merged 2 commits into
canonical:masterfrom
3v1n0:snap-app-env-vars

Conversation

@3v1n0

@3v1n0 3v1n0 commented Sep 19, 2025

Copy link
Copy Markdown
Contributor

When running an application of a snap is complicated to get some per-app state values such as its name, desktop file and common IDs, but these are actually important to make the application to be able to identify itself properly in a desktop environment.

As per this, expose the application information as env variables.

Related to: https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/2125222

@github-actions github-actions Bot added the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Sep 19, 2025
@github-actions

github-actions Bot commented Sep 19, 2025

Copy link
Copy Markdown

Mon Jun 1 06:32:57 UTC 2026
The following results are from: https://github.com/canonical/snapd/actions/runs/26530136373

Failures:

Preparing:

  • google-pro:ubuntu-fips-22.04-64:
  • google-pro:ubuntu-fips-22.04-64:

Executing:

  • openstack:opensuse-tumbleweed-64:tests/main/security-logging
  • openstack:opensuse-tumbleweed-selinux-64:tests/main/security-logging
  • openstack:opensuse-16.0-64:tests/main/security-logging
  • garden:ubuntu-core-18-64:tests/core/auto-refresh-backoff-after-reboot:kernel

Restoring:

  • google-pro:ubuntu-fips-22.04-64:
  • google-pro:ubuntu-fips-22.04-64:

Skipped tests from snapd-testing-skip

If you wish to have any of the below tests run in your PR, in your PR description, add 'unskip:' followed by a copy-and-pasted list (without variants) of the below tests you wish to run (unskip plus test list must be valid yaml)

  • garden:ubuntu-25.10-64:tests/main/apparmor-prompting-support
  • openstack-arm:ubuntu-24.04-arm-64:tests/main/i18n
  • openstack-arm:ubuntu-24.04-arm-64:tests/main/snapctl-is-ready
  • openstack-arm:ubuntu-24.04-arm-64:tests/main/snapctl-no-wait
  • openstack-arm:ubuntu-core-24-arm-64:tests/main/i18n
  • openstack-arm:ubuntu-core-24-arm-64:tests/main/snapctl-is-ready
  • openstack-arm:ubuntu-core-24-arm-64:tests/main/snapctl-no-wait
  • openstack:amazon-linux-2-64:tests/main/snapctl-no-wait
  • openstack:amazon-linux-2023-64:tests/main/snapctl-no-wait
  • openstack:arch-linux-64:tests/main/snapctl-no-wait
  • openstack:centos-9-64:tests/main/snapctl-no-wait
  • openstack:debian-12-64:tests/main/snapctl-no-wait
  • openstack:debian-sid-64:tests/main/interfaces-network-status-classic
  • openstack:debian-sid-64:tests/main/snapctl-no-wait
  • openstack:fedora-44-64:tests/main/snapctl-is-ready
  • openstack:fedora-44-64:tests/main/snapctl-no-wait
  • openstack:opensuse-16.0-64:tests/main/snapctl-no-wait
  • openstack:opensuse-tumbleweed-64:tests/main/snapctl-no-wait
  • openstack:opensuse-tumbleweed-selinux-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-16.04-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-16.04-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-18.04-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-18.04-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-20.04-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-20.04-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-22.04-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-22.04-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-24.04-64:tests/main/i18n
  • openstack:ubuntu-24.04-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-24.04-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-flag-restart
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:audio_record_single
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:audio_record_timespan_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:audio_record_timespan_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_multiple_actioned_by_other_pid_always_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_multiple_actioned_by_other_pid_always_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_multiple_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_multiple_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_multiple_not_actioned_by_other_pid_single_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_multiple_not_actioned_by_other_pid_single_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_write_chmod_same_fd_single_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_write_chmod_same_path_single_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:create_write_write_same_path_single_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:download_file_conflict
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:download_file_defaults
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:download_file_safer
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:read_single_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:read_single_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:special_characters
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:timespan_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:timespan_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:write_read_multiple_actioned_by_other_pid_allow_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:write_read_multiple_actioned_by_other_pid_deny_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:write_single_allow
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-integration-tests:write_single_deny
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-prompt-restoration
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:audiorecord_allow_forever
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:audiorecord_allow_session
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:audiorecord_allow_single
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:audiorecord_allow_timespan
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:audiorecord_deny_forever
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:audiorecord_deny_session
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:audiorecord_deny_single
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:audiorecord_deny_timespan
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:camera_allow_forever
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:camera_allow_session
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:camera_allow_single
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:camera_allow_timespan
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:camera_deny_forever
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:camera_deny_session
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:camera_deny_single
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:camera_deny_timespan
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:home_allow_forever
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:home_allow_session
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:home_allow_single
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:home_allow_timespan
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:home_deny_forever
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:home_deny_session
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:home_deny_single
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-smoke:home_deny_timespan
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-snapd-startup
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-support
  • openstack:ubuntu-25.10-64:tests/main/interfaces-requests-activates-handlers
  • openstack:ubuntu-25.10-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-25.10-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-flag-restart
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:audio_record_single
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:audio_record_timespan_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:audio_record_timespan_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_multiple_actioned_by_other_pid_always_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_multiple_actioned_by_other_pid_always_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_multiple_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_multiple_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_multiple_not_actioned_by_other_pid_single_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_multiple_not_actioned_by_other_pid_single_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_write_chmod_same_fd_single_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_write_chmod_same_path_single_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:create_write_write_same_path_single_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:download_file_conflict
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:download_file_defaults
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:download_file_safer
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:read_single_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:read_single_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:special_characters
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:timespan_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:timespan_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:write_read_multiple_actioned_by_other_pid_allow_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:write_read_multiple_actioned_by_other_pid_deny_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:write_single_allow
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-integration-tests:write_single_deny
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-prompt-restoration
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:audiorecord_allow_forever
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:audiorecord_allow_session
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:audiorecord_allow_single
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:audiorecord_allow_timespan
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:audiorecord_deny_forever
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:audiorecord_deny_session
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:audiorecord_deny_single
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:audiorecord_deny_timespan
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:camera_allow_forever
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:camera_allow_session
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:camera_allow_single
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:camera_allow_timespan
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:camera_deny_forever
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:camera_deny_session
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:camera_deny_single
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:camera_deny_timespan
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:home_allow_forever
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:home_allow_session
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:home_allow_single
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:home_allow_timespan
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:home_deny_forever
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:home_deny_session
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:home_deny_single
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-smoke:home_deny_timespan
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-snapd-startup
  • openstack:ubuntu-26.04-64:tests/main/apparmor-prompting-support
  • openstack:ubuntu-26.04-64:tests/main/i18n
  • openstack:ubuntu-26.04-64:tests/main/interfaces-requests-activates-handlers
  • openstack:ubuntu-26.04-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-26.04-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-core-18-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-core-18-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-core-20-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-core-20-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-core-22-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-core-22-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-core-24-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-core-24-64:tests/main/snapctl-no-wait
  • openstack:ubuntu-core-26-64:tests/main/snapctl-is-ready
  • openstack:ubuntu-core-26-64:tests/main/snapctl-no-wait

@codecov

codecov Bot commented Sep 19, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.99%. Comparing base (156d777) to head (b35217b).
⚠️ Report is 22 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #16016   +/-   ##
=======================================
  Coverage   78.98%   78.99%           
=======================================
  Files        1381     1377    -4     
  Lines      191967   191970    +3     
  Branches     2464     2464           
=======================================
+ Hits       151630   151641   +11     
- Misses      31184    31185    +1     
+ Partials     9153     9144    -9     
Flag Coverage Δ
unittests 78.99% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread snap/snapenv/snapenv.go Outdated
Comment thread snap/snapenv/snapenv.go
// Set various SNAP_ environment variables as well as some non-SNAP variables,
// depending on snap confinement mode. Note that this does not include environment
// set by snap-exec.
for k, v := range snapEnv(info, component, opts) {
for k, v := range snapEnv(info, app, component, opts) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to have similar information for hooks?

type HookInfo struct {
	Snap *Info

	// Component will be nil if the hook is not a component hook.
	Component *Component

	Name  string
	Plugs map[string]*PlugInfo
	Slots map[string]*SlotInfo

	Environment  strutil.OrderedMap
	CommandChain []string

	Explicit bool
}

looks like we could have SNAP_HOOK=<hook-name>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, look like they could be useful too, although if an hook is running I assume it know that it knows what its own name?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in theory the same applies to apps 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... Not much, in the sense that while hooks are designed to be inside a snap, an application can be embedded into anyof snap

@bboozzoo
bboozzoo requested review from pedronis and zyga September 22, 2025 10:36
@3v1n0
3v1n0 requested a review from bboozzoo September 22, 2025 11:50
Comment thread snap/snapenv/snapenv.go Outdated
Comment thread snap/snapenv/snapenv.go
Comment thread snap/snapenv/snapenv.go Outdated
Comment thread snap/snapenv/snapenv.go Outdated
Comment thread snap/snapenv/snapenv.go Outdated
Comment thread snap/snapenv/snapenv.go Outdated
Comment thread snap/snapenv/snapenv.go
@bboozzoo bboozzoo self-assigned this Sep 26, 2025
@ZeyadYasser
ZeyadYasser self-requested a review October 17, 2025 12:16
@3v1n0
3v1n0 force-pushed the snap-app-env-vars branch from c9bbf46 to fba145d Compare October 29, 2025 00:15
@olivercalder
olivercalder self-requested a review November 4, 2025 15:52
@3v1n0
3v1n0 force-pushed the snap-app-env-vars branch 2 times, most recently from a33207e to 301f31b Compare February 17, 2026 20:32
@3v1n0

3v1n0 commented Feb 17, 2026

Copy link
Copy Markdown
Contributor Author

@olivercalder if you can give a look it would be appreciated too :)

@pedronis
pedronis removed the request for review from ZeyadYasser February 26, 2026 13:30
@bboozzoo
bboozzoo requested review from bboozzoo and zyga April 2, 2026 08:12

@olivercalder olivercalder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Just needs tests to be expanded a bit more, I think.

Comment thread snap/snapenv/snapenv_test.go Outdated
Comment thread snap/snapenv/snapenv_test.go
MATCH "^SNAP_CONTEXT=$CTX" < snap-vars.txt
# parallel-installs: $SNAP_NAME is always _the_ snap name
MATCH '^SNAP_NAME=test-snapd-tools$' < snap-vars.txt
MATCH '^SNAP_APP_NAME=env$' < snap-vars.txt

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to test a snap which has all the new variables set.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh... Could you hep with that? Not being able to run the spread tests myself makes it a bit annoying to do...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can run them with run-spread garden:... just install image-garden snap.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test-snapd-desktop-file-ids should do the trick, but needs a rebase on master. I'll poke at this and see what I can do.

@3v1n0
3v1n0 force-pushed the snap-app-env-vars branch 3 times, most recently from 60791f2 to 22eb166 Compare April 29, 2026 12:27
@zyga
zyga requested a review from Copilot May 27, 2026 15:59

@zyga zyga left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Please expand the integration test to show all variables at least once.

Please update the documentation, especially on what to do as an app developer to get the correct experience.

Comment thread snap/snapenv/snapenv.go
"SNAP_APP_NAME": app.Name,
}

if app.CommonID != "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all very welcome. We must document it somewhere.

MATCH "^SNAP_CONTEXT=$CTX" < snap-vars.txt
# parallel-installs: $SNAP_NAME is always _the_ snap name
MATCH '^SNAP_NAME=test-snapd-tools$' < snap-vars.txt
MATCH '^SNAP_APP_NAME=env$' < snap-vars.txt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can run them with run-spread garden:... just install image-garden snap.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the snap run-time environment to expose per-application identity/state (not just per-snap), so desktop environments and app integrations can reliably determine which snap app is running (per Launchpad #2125222).

Changes:

  • Add app-scoped SNAP_* variables (starting with SNAP_APP_NAME, plus optional common ID, bus name, and desktop file path) to the environment assembled for snap run.
  • Thread the app context into snapenv.ExtendEnvForRun(...) from snap run so the env can be populated for app runs while remaining nil-safe for hooks/components.
  • Update unit and spread tests to validate SNAP_APP_NAME and adjust expected variable counts.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
tests/main/snap-env/task.yaml Asserts SNAP_APP_NAME is present and updates expected SNAP_* variable counts.
snap/snapenv/snapenv.go Adds app-level env population (SNAP_APP_*) and updates the run-env API to accept an app context.
snap/snapenv/snapenv_test.go Adds unit tests covering app env vars, including desktop-file resolution scenarios.
cmd/snap/cmd_run.go Passes the runnable app context into snapenv.ExtendEnvForRun when building the execution environment.

When running an application of a snap is complicated to get some per-app
state values such as its name, desktop file and common IDs, but these
are actually important to make the application to be able to identify
itself properly in a desktop environment.

As per this, expose the application information as env variables.

Related to: https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/2125222

@olivercalder olivercalder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can properly test things now with the test-snapd-desktop-file-ids snap.

MATCH "^SNAP_CONTEXT=$CTX" < snap-vars.txt
# parallel-installs: $SNAP_NAME is always _the_ snap name
MATCH '^SNAP_NAME=test-snapd-tools$' < snap-vars.txt
MATCH '^SNAP_APP_NAME=env$' < snap-vars.txt

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test-snapd-desktop-file-ids should do the trick, but needs a rebase on master. I'll poke at this and see what I can do.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

@olivercalder olivercalder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rebased and extended the spread test slightly, otherwise looks good to me.

MATCH '^SNAP_NAME=test-snapd-desktop-file-ids$' < snap-vars-desktop.txt
MATCH '^SNAP_APP_NAME=cmd$' < snap-vars-desktop.txt
MATCH '^SNAP_APP_COMMON_ID=org.example.Foo$' < snap-vars-desktop.txt
MATCH '^SNAP_APP_DESKTOP_FILE=/var/lib/snapd/desktop/applications/org.example.Foo.desktop$' < snap-vars-desktop.txt

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT there are no existing spread tests which check the bus name, except a few use python scripts, which seems a stretch to use here. But we can at least check the other new desktop metadata.

@olivercalder
olivercalder requested a review from pedronis May 27, 2026 18:21
@pedronis pedronis added this to the 2.77 milestone May 28, 2026

@pedronis pedronis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, @bboozzoo we need to remember to document this, maybe you can create a task for that?

@bboozzoo bboozzoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@bboozzoo

Copy link
Copy Markdown
Contributor

thanks, @bboozzoo we need to remember to document this, maybe you can create a task for that?

even better, I've opened canonical/snap-docs#354

@olivercalder olivercalder added the Auto rerun spread Auto reruns spread up to 4 times in non-draft PRs w/ >=1 approval and <20 fails in any fund. system label May 29, 2026
@bboozzoo
bboozzoo merged commit 409deb4 into canonical:master Jun 2, 2026
597 of 632 checks passed
@3v1n0 3v1n0 mentioned this pull request Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Auto rerun spread Auto reruns spread up to 4 times in non-draft PRs w/ >=1 approval and <20 fails in any fund. system Needs Documentation -auto- Label automatically added which indicates the change needs documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants